package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.MessageListEntity;
import com.zw.yzk.kabu.data.network.task.MessageListTask;
import com.zw.yzk.kabu.data.network.task.ReadMessageTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.MessageMapper;
import com.zw.yzk.kabu.view.view.MessageView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class LightListPresenter implements Presenter {

    private UseCase getMessageList;
    private UseCase readMessage;
    private AppCompatActivity activity;
    private MessageView messageView;
    private MessageMapper mapper;

    @Inject
    public LightListPresenter(@Named("messageListUseCase") UseCase getMessageList,
                              @Named("readMessageUseCase") UseCase readMessage,
                              AppCompatActivity activity, MessageMapper mapper) {
        this.getMessageList = getMessageList;
        this.readMessage = readMessage;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(MessageView view) {
        this.messageView = view;
    }

    public void getMessageList(int start, int lastId) {
        getMessageList.execute(new GetMessageListSubscriber(activity), new MessageListTask(start, lastId, MessageListTask.MESSAGE_LIGHT));
    }

    public void readMessage() {
        ReadMessageTask task = new ReadMessageTask();
        task.type = MessageListTask.MESSAGE_LIGHT;
        readMessage.execute(new ReadMessageSubscriber(activity), task);
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getMessageList.unSubscribe();
        readMessage.unSubscribe();
    }

    private class GetMessageListSubscriber extends BaseSubscriber<MessageListEntity> {

        public GetMessageListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_message_list_failed);
        }

        @Override
        public void onNext(MessageListEntity entity) {
            super.onNext(entity);
            messageView.getMessageSuccess(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            messageView.getMessageFailed();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }

    private class ReadMessageSubscriber extends BaseSubscriber<Boolean> {

        public ReadMessageSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }

        @Override
        public void onError(Throwable e) {
        }

    }
}
